package com.swf.common.service;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

import org.slf4j.*;

public class JspContextService {
	private static Logger logger = LoggerFactory
			.getLogger(JspContextService.class);

	private HttpServletRequest request;
	private HttpServletResponse response;

	/**
	 * 返回实例，未初始化
	 * 
	 * @return
	 */
	public static JspContextService getInstance() {
		return new JspContextService();
	}

	/**
	 * 创建实例，并初始化
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	public static JspContextService getInstance(HttpServletRequest request,
			HttpServletResponse response)  {
		JspContextService service = new JspContextService();
		service.initialize(request, response);
		return service;
	}

	/**
	 * 初始化
	 * 
	 * @param request
	 * @param response
	 */
	public void initialize(HttpServletRequest request,
			HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	/**
	 * @param request
	 * @return 请求的相对路径
	 */
	public String getBasepath() {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";
		return basePath;
	}
	
	/**
	 * 不传递参数
	 * 跳转到指定页面
	 * @param relativePath 相对路径，不以/开始，相对与项目路径
	 */
	public void sendRedirect(String relativePath){
		try {
			response.sendRedirect(getBasepath()+relativePath);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
	}
	/**
	 * 传递参数
	 * 跳转到指定页面
	 * @param relativePath 相对路径，不以/开始，相对与项目路径
	 */
	public void forward(String relativePath){
		try {
			request.getRequestDispatcher(relativePath).forward(request, response);
		} catch (ServletException e) {
			logger.error(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @param request
	 * @return HttpSession 返回会话
	 */
	public HttpSession getSession() {
		return request.getSession();
	}

	/**
	 * @param request
	 * @return 返回当前请求发送的cookies
	 */
	public Cookie[] getCookies() {
		Cookie[] cookies = request.getCookies();
		return cookies;
	}

	/**
	 * @param request
	 * @return 返回上下文
	 */
	public ServletContext getServletContext() {
		return request.getSession().getServletContext();
	}

	/**
	 * @param session
	 * @return 返回上下文
	 */
	public ServletContext getServletContext(HttpSession session) {
		return session.getServletContext();
	}
	
	public void setAttribute(String key,Object value){
		request.setAttribute(key,  value);
	}
	
	/**
	 *  通过key（表单提交的name）获取request中的参数
	 * @param key
	 * @return
	 */
	public String get(String key){
		return request.getParameter(key);
	}
	
	/**
	 * 获取request的属性值
	 * @param key
	 * @return
	 */
	public Object getRequestAttribute(String key){
		return request.getAttribute(key);
	}
	
	/**
	 * 获取session中的属性值
	 * @param key
	 * @return
	 */
	public Object getSessionAttribute(String key){
		return request.getSession().getAttribute(key);
	}
	
	public String getButton(String buttonName,String displayName,String jsFunction){
		StringBuffer sb=new StringBuffer();
		sb.append("<input type=\"button\"");
		sb.append(" id=\""+buttonName);
		sb.append(" value=\""+getDisplay(displayName)+"\"");
		sb.append(" onclick=\""+jsFunction+"\"");
		return sb.toString();
	}

	public String getDisplay(String displayName) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
